<?php

namespace App\Http\Middleware;

use App\Models\Material;
use App\Models\Rbac\User;
use App\Notifications\MaterialNotify;
use Closure;
use Illuminate\Http\Request;

class CheckMaterial
{
    /**
     * 检查物料库存
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $material = Material::where('is_del',2)->whereRaw('initial_now<initial_min')->get();
        //p($material);
        if($material->count()>0){
            foreach($material as $k=>$v){
                $unread_count = $v->unreadNotifications()->count();
                if($unread_count == 0){}
                $v->notify(new MaterialNotify($v));
            }
        }
        return $next($request);
    }
}
